import requests
from calendar import Calendar
import time


def saveData(item):
    print(item)
    headers = {
        'Cookie': 'JSESSIONID=4D47666196EE493216EACADED62D46B8'
    }

    data = {'DIYBegin': item.strftime("%Y-%m-%d") + '+00', 'DIYEend': item.strftime("%Y-%m-%d") + '+23',
            'method': 'DIYTimeChange', 'path': '/meterSearchListPage/dateSelectSearchMeterPanel'}
    print(data)
    res = requests.post("http://nhpt.nbu.edu.cn/panel/dateSelectPanel.do", data, headers=headers)
    print(res.status_code)
    print(res.content)
    r = requests.get(
        "http://nhpt.nbu.edu.cn/panel/listPanel.do?path=/meterSearchListPage/meterSearchListPanel&method=export&epType=xls&suffix=xls",
        headers=headers)
    with open("2020/" + item.strftime("%Y-%m-%d") + ".xls", "wb") as code:
        code.write(r.content)
    time.sleep(3)


if __name__ == '__main__':

    c = Calendar(firstweekday=6)

    month = 1
    while (month < 13):
        for item in c.itermonthdates(2020, month):
            if month == item.month:
                saveData(item)
        month = month + 1
